package com.example.controller;

import com.example.util.IpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@Slf4j
public class ReturnStrDemo {
    private String testwords = "hello-master费曼学习";

    @ResponseBody
    @RequestMapping(value = "/check/{str}", method = RequestMethod.GET)
    public String returnStr(@PathVariable("str") String str) {
        log.info("realIp:" + IpUtil.getRealIp() + " getBack：" + str + "--健康检查" + testwords);
        return getTime() + ":realIp:" + IpUtil.getRealIp() + " getBack：" + str + "--健康检查" + testwords;
    }

    @RequestMapping(value = "/getIp", method = RequestMethod.GET)
    public String getIp() {
        return "hostIp：" + IpUtil.getHostAddress() + "\r\nrealIp:" + IpUtil.getRealIp();
    }

    private static String getTime() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDate = now.format(formatter);
        //  System.out.println("当前时间: " + formattedDate);
        return formattedDate;
    }


    @Autowired
    private Environment environment;

    @Autowired
    private RestTemplate restTemplate;

    @ResponseBody
    @RequestMapping(value = "/invoke/{str}", method = RequestMethod.GET)
    public String invoke(@PathVariable("str") String str) {
        String ip = environment.getProperty("ip_b");
        log.info("pod_a(" + IpUtil.getRealIp() + ") begin invoking service_b_ip:" + ip);
        String url = "http://" + ip + ":12560/b/" + str;
        String result = "pod-a(" + IpUtil.getRealIp() + ") invoke " + restTemplate.getForObject(url, String.class);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/b/{str}", method = RequestMethod.GET)
    public String invokeB(@PathVariable("str") String str) {
        String ip_c = environment.getProperty("ip_c");
        String ip_d = environment.getProperty("ip_d");
        String delay_c = environment.getProperty("delay_c");
        String delay_d = environment.getProperty("delay_d");
        if("c".equals(str)) {
            log.info("pod_b(" + IpUtil.getRealIp() + ") begin invoking service_c_ip:" + ip_c);
            String url_c = "http://" + ip_c + ":12560/c/" + delay_c;
            return "pod_b(" + IpUtil.getRealIp() + "),pod_b(" + IpUtil.getRealIp() + ") invoke pod_c:(" + restTemplate.getForObject(url_c, String.class) + ",delay"+delay_c+"ms)";
        }else if ("d".equals(str)) {
            log.info("pod_b(" + IpUtil.getRealIp() + ") begin invoking service_d_ip:" + ip_d);
            String url_d = "http://" + ip_d + ":12560/d/" + delay_d;
            return "pod_b(" + IpUtil.getRealIp() + "),pod_b(" + IpUtil.getRealIp() + ") invoke pod_d:(" + restTemplate.getForObject(url_d, String.class) + ",delay"+delay_d+"ms)";
        }else if("cd".equals(str)) {
            log.info("pod_b(" + IpUtil.getRealIp() + ") begin invoking service_c_ip:" + ip_c);
            String url_c = "http://" + ip_c + ":12560/c/" + delay_c;
            String result_c = restTemplate.getForObject(url_c, String.class);
            log.info("pod_b(" + IpUtil.getRealIp() + ") begin invoking service_d_ip:" + ip_d);
            String url_d = "http://" + ip_d + ":12560/d/" + delay_d;
            String result_d = restTemplate.getForObject(url_d, String.class);
            return  "pod_b(" + IpUtil.getRealIp() + "),pod_b(" + IpUtil.getRealIp() + ") invoke pod_c:(" + result_c + ",delay"+delay_c+"ms) and pod_d:(" + result_d + ",delay"+delay_d+"ms)";
        }else {
            return "end by b-service,over!";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/c/{time}", method = RequestMethod.GET)
    public String invokeC(@PathVariable("time") int time) throws InterruptedException {
        log.info("pod_c(" + IpUtil.getRealIp() + ") is being invoked");
        Thread.sleep(time);
        String result = IpUtil.getRealIp();
        return result;
    }
    @ResponseBody
    @RequestMapping(value = "/d/{time}", method = RequestMethod.GET)
    public String invokeD(@PathVariable("time") int time) throws InterruptedException {
        log.info("pod_d(" + IpUtil.getRealIp() + ") is being invoked");
        Thread.sleep(time);
        String result = IpUtil.getRealIp();
        return result;
    }

    public static void main(String[] args) {
        System.out.println("str".substring(1, 2));
    }
}
